/*
 * SyncSubService.java
 *
 * Created on 29 March 2012, 16:31
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package Simulation.Services.Common;

import Simulation.Config.Service.ServiceConfigReader;
import Simulation.Messages.Message;
import Simulation.Messages.MessageCommonConstants;
import Simulation.Messages.PerformanceMessage;

/**
 *
 * @author aking2
 */
public class SyncSubService extends Thread {
    
    /** Creates a new instance of SyncSubService */
    public SyncSubService(byte simId) {   
        
        simulatorId = simId;
        
        System.out.println("Sync SubService Loading Config");   
        config = new ServiceConfigReader("Simulation/Config/Service/SyncSubServiceConfig.txt");
        
        System.out.println("Sync SubService Creating inter service writer socket"); 
        comms = new ServiceComms();        
        
        System.out.println("Sync SubService Adding network destinations");   
        String datum = config.GetData("INTER_SERVICE_NUM_OF_NETWORK_DESTINATIONS");
        int num_of_network_destiantions = Integer.parseInt(datum);
        for( int i=0; i<num_of_network_destiantions; i++) {
            comms.AddInterServiceDestiantion(
                config.GetData("INTER_SERVICE_NETWORK_DESTINATIONS_IP_" + String.valueOf(i+1)),
                Integer.parseInt(config.GetData("INTER_SERVICE_NETWORK_DESTINATIONS_PORT_" + String.valueOf(i+1))));
        }   
        
        System.out.println("Sync SubService Adding IOS network destinations");   
        String datum2 = config.GetData("IOS_NUM_OF_NETWORK_DESTINATIONS");
        int num_of_ios_destiantions = Integer.parseInt(datum2);
        for( int i=0; i<num_of_ios_destiantions; i++) {
            comms.AddIosDestiantion(
                config.GetData("IOS_NETWORK_DESTINATIONS_IP_" + String.valueOf(i+1)),
                Integer.parseInt(config.GetData("IOS_NETWORK_DESTINATIONS_PORT_" + String.valueOf(i+1))));
        }            
        
        cigiTimeoutSecs       = Float.parseFloat(config.GetData("CIGI_TIMEOUT_SECS"));
        processInputDelaySecs = Float.parseFloat(config.GetData("PROCESS_INPUT_DELAY_SECS"));
        SendOutputDelaySecs   = Float.parseFloat(config.GetData("SEND_OUTPUT_DELAY_SECS"));
        
        monitiorPerformance = config.GetData("SETTINGS_MONITOR_PERFORMANCE").equalsIgnoreCase("True");
    }
    
    
    public void run() {

        System.out.println("Sync Sub Service Running");
        while( running ) {
            try {                
                // Wait until we get a start of frame message
                Thread.currentThread().sleep((int) (1000.0 * cigiTimeoutSecs));
                
                // Send the SEND_LAST_TO_CIGI message
                comms.SendMessage(new Message(Message.MessageIdEnum.SYNC_SEND_LAST_TO_CIGI));
                            
                // Send the SEND_LAST_TO_DIS message 
                comms.SendMessage(new Message(Message.MessageIdEnum.SYNC_SEND_LAST_TO_DIS));            
                
                // Send the PROCESS_INPUTS message after a pause
                Thread.currentThread().sleep((int) (1000.0 * processInputDelaySecs));
                
                /*
                 * if monitoring performance, send a message to the IOS with timing information
                 * This will act as a reference point for the other propper services that
                 * will be responding.
                 */
                if(monitiorPerformance) {
                    comms.SendMessage( new PerformanceMessage(
                      simulatorId,
                      MessageCommonConstants.SYNC_SUB_SERVICE,
                      PerformanceMessage.PERFORMANCE_PROCESS_INPUTS,
                      System.nanoTime(),
                      0));               
                }                                
                comms.SendMessage(new Message(Message.MessageIdEnum.SYNC_PROCESS_INPUTS));   
                

                                                
                // Send the SEND_OUTPUTS message after a pause
                Thread.currentThread().sleep((int) (1000.0 * SendOutputDelaySecs));
                /*
                 * if monitoring performance, send a message to the IOS with timing information
                 * This will act as a reference point for the other propper services that
                 * will be responding.
                 */
                if(monitiorPerformance) {
                    comms.SendMessage( new PerformanceMessage(
                      simulatorId,
                      MessageCommonConstants.SYNC_SUB_SERVICE,
                      PerformanceMessage.PERFORMANCE_SEND_OUTPUTS,
                      System.nanoTime(),
                      0));               
                }                 
                comms.SendMessage(new Message(Message.MessageIdEnum.SYNC_SEND_OUTPUTS));
                
               
                
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }                                       
        }
    }  
    

    
    private boolean        running = true;
    private ServiceConfigReader   config;
    private ServiceComms   comms;   
    private float          cigiTimeoutSecs;
    private float          processInputDelaySecs;
    private float          SendOutputDelaySecs; 
    private boolean        monitiorPerformance;
    private byte           simulatorId;

}
